from datetime import datetime

import peewee as pw
# 数据库连接配置
db = pw.MySQLDatabase(
    'qkl',
    user='root',
    password='1473992017',
    host='localhost',
    port=3306
)

# 定义数据模型
class BaseModel(pw.Model):
    class Meta:
        database = db

class User(BaseModel):
    username = pw.CharField(max_length=50, unique=True)
    password = pw.CharField(max_length=64)  # 存储SHA-256哈希值
    email = pw.CharField(max_length=100, null=True)
    created_at = pw.DateTimeField(default=datetime.now)
    last_login = pw.DateTimeField(null=True)

    class Meta:
        table_name = 'users'

class Transaction(BaseModel):
    tx_hash = pw.CharField(max_length=66)
    block_height = pw.IntegerField()
    timestamp = pw.BigIntegerField()
    from_address = pw.CharField(max_length=42)
    to_address = pw.CharField(max_length=42)
    value = pw.FloatField()
    is_error = pw.BooleanField(default=False)
    created_at = pw.DateTimeField(default=datetime.now)

    class Meta:
        table_name = 'transactions'

# 创建数据库表
def create_tables():
    with db:
        db.create_tables([User, Transaction], safe=True)
